package com.ken.apollo.controller;

import com.ken.apollo.commons.result.R;
import com.ken.apollo.domain.bo.CarouselImageBo;
import com.ken.apollo.domain.query.CarouselImageQuery;
import com.ken.apollo.domain.vo.CarouselImageVo;
import com.ken.apollo.service.CarouselImageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * 轮播图表(CarouselImage)表控制层
 *
 * @author Ken
 * @since 2025-11-03
 */
@Slf4j
@RestController
@RequestMapping("/api/carousel/")
public class CarouselImageController {
    
    @Resource
    private CarouselImageService carouselImageService;

    /**
     * 新增轮播图
     */
    @PostMapping("/save")
    public R<Boolean> save(CarouselImageBo carouselImageBo) {
        try {
            return R.success(carouselImageService.save(carouselImageBo));
        } catch (Exception e) {
            log.error("【新增轮播图失败】", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新轮播图
     */
    @PostMapping("/update")
    public R<Boolean> update(CarouselImageBo carouselImageBo) {
        try {
            return R.success(carouselImageService.update(carouselImageBo));
        } catch (Exception e) {
            log.error("【更新轮播图失败】", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询轮播图列表
     */
    @GetMapping("/list")
    public R<List<CarouselImageVo>> list(CarouselImageQuery carouselImageQuery) {
        try {
            return R.success(carouselImageService.list(carouselImageQuery));
        } catch (Exception e) {
            log.error("【查询轮播图列表失败】", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 根据轮播图ID查询轮播图信息
     */
    @GetMapping("/getById")
    public R<CarouselImageVo> getById(Long id) {
        try {
            return R.success(carouselImageService.getById(id));
        } catch (Exception e) {
            log.error("【根据ID查询轮播图失败】", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除轮播图（单个）
     */
    @PostMapping("/delete")
    public R<Boolean> delete(@RequestParam("id") Long id) {
        try {
            return R.success(carouselImageService.removeById(id));
        } catch (Exception e) {
            log.error("【删除轮播图失败】id={}", id, e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除轮播图
     */
    @PostMapping("/batchDelete")
    public R<Boolean> batchDelete(@RequestParam("ids") Collection<Long> ids) {
        try {
            return R.success(carouselImageService.removeByIds(ids));
        } catch (Exception e) {
            log.error("【批量删除轮播图失败】", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新轮播图状态
     */
    @PostMapping("/updateStatus")
    public R<Boolean> updateStatus(@RequestParam("id") Long id, @RequestParam("status") Boolean status) {
        try {
            return R.success(carouselImageService.updateStatus(id, status));
        } catch (Exception e) {
            log.error("【更新轮播图状态失败】", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新轮播图排序
     */
    @PostMapping("/updateSortOrder")
    public R<Boolean> updateSortOrder(@RequestParam("id") Long id, @RequestParam("sortOrder") Integer sortOrder) {
        try {
            return R.success(carouselImageService.updateSortOrder(id, sortOrder));
        } catch (Exception e) {
            log.error("【更新轮播图排序失败】", e);
            return R.fail(e.getMessage());
        }
    }
}

